package com.copera.cn.kaka.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.copera.cn.kaka.ApplicationCustomer;
import com.copera.cn.kaka.R;
import com.copera.cn.kaka.util.ActionUrls;
import com.copera.cn.kaka.util.AsyncImageLoader;
import com.copera.cn.kaka.util.Utility;
import com.copera.cn.kaka.util.AsyncImageLoader.ImageCallBack;
import com.copera.cn.kaka.vo.ImageVO;

public class ShopFootImageAdapter extends BaseAdapter {
	
	
	private List<ImageVO> items;
	
	private Context context;  
	  
    private LayoutInflater mInflater; 
    private AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
    private ApplicationCustomer appc;
	private Handler handler;
	private int displayWidth;
	
	public ShopFootImageAdapter(List<ImageVO> arrayList, Context context,ApplicationCustomer appc, Handler handler,int displayWidth){
		this.items = arrayList;
		this.context = context;
		this.appc = appc;
		this.handler = handler;
		this.displayWidth = displayWidth;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		
		if(convertView == null){
			viewHolder = new ViewHolder();
			mInflater = LayoutInflater.from(context);
			convertView = mInflater.inflate(R.layout.item_shop_image, null);
			
			viewHolder.banner = (ImageView)convertView.findViewById(R.id.banner);
			//viewHolder.type = (TextView)convertView.findViewById(R.id.type);
			//viewHolder.id = (TextView)convertView.findViewById(R.id.id);
			viewHolder.title = (TextView)convertView.findViewById(R.id.title);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
			viewHolder.banner = (ImageView)convertView.findViewById(R.id.banner);
			//viewHolder.type = (TextView)convertView.findViewById(R.id.type);
			//viewHolder.id = (TextView)convertView.findViewById(R.id.id);
			viewHolder.title = (TextView)convertView.findViewById(R.id.title);
		}
		final ImageVO vo = items.get(position);
		//viewHolder.type.setText(vo.getType());
		viewHolder.title.setText(vo.getTitle().equals("无")?"":vo.getTitle());
		Bitmap bitmap = asyncImageLoader.loadBitmap(viewHolder.banner,
				ActionUrls.HOST_IMG+vo.getFilepath(), appc.getHttpClient(), new ImageCallBack() {

					
					@Override
					public void imageLoad(ImageView imageView, Bitmap bitmap) {
						bitmap=Utility.change(bitmap, displayWidth);
						BitmapDrawable bd=new BitmapDrawable(bitmap); 
						imageView.setBackgroundDrawable(bd);
					}
				});
		if (bitmap != null) {
			bitmap=Utility.change(bitmap, displayWidth);
			BitmapDrawable bd=new BitmapDrawable(bitmap); 
			viewHolder.banner.setBackgroundDrawable(bd);
		}
		
	       
		return convertView;
	}
	
	private class ViewHolder{
		
		ImageView banner;
		TextView type,id,title;
	}
	
	public void addItem(ImageVO cartVO){
		if(cartVO != null){
			items.add(cartVO);
		}
	}
	
	public void clear(){
		items.clear();
	}
}
